package com.nintendo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nintendo.dto.DocDTO;
import com.nintendo.entity.Doc;
import com.nintendo.mapper.DocMapper;
import com.nintendo.service.DocService;
import com.nintendo.util.SnowFlake;
import com.nintendo.vo.DocVo;
import com.nintendo.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DocServiceImpl implements DocService {
    @Autowired
    private DocMapper docsMapper;
    @Autowired
    private SnowFlake snowflake;
    @Override
    public List<Doc> findAll() {
        return docsMapper.findAll();
    }

    @Override
    public PageInfoVo findByNameAuthPage(DocDTO docDTO) {
        PageHelper.startPage(docDTO.getPageNum(), 1000);
        List<DocVo> docList = docsMapper.findByNameAuthPage(docDTO);
        PageInfo<DocVo> pageInfo = new PageInfo<>(docList);
        return new PageInfoVo(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void updateDocById(Doc doc) {
        docsMapper.updateDocById(doc);
    }

    @Override
    public Doc findDocById(Long id) {
        return docsMapper.findDocById(id);
    }

    @Override
    public void deleteDocById(Long id) {
        docsMapper.deleteById(id);
    }

    @Override
    public List<Doc> findDocItemsById(Long ebookId) {
        return docsMapper.findDocItemsById(ebookId);
    }

    @Override
    public void saveDoc(Doc doc) {
        if (doc.getId() !=0) {
            docsMapper.updateDocById(doc);
        } else {
            Long nextId = snowflake.nextId();
            String nextIdS = nextId.toString();
            doc.setId(Long.valueOf(nextIdS.substring(8, nextIdS.length())));
            docsMapper.saveDoc(doc);
        }
    }

    @Override
    public void deleteDocByIds(List<Long> ids) {
        docsMapper.deleteDocByIds(ids);
    }
}
